/**
* webplayer for vod
* Author: Feil.Wang
* Date: 2013.07
*/

;(function(window,undefined){

	window.webplayer = {
		winWidth : "",
		winHeight : "",

		init : function(){
			this.winSizeLimit();
			this.bgReSize();
			this.cookieLayout();
		},
		resizeInit : function(){
			this.winSizeLimit();
			this.bgReSize();
			this.defaultLayout();
		},
		winSizeLimit : function(){
			var _w = $(window).width(),
				_h = $(window).height();
			if(_w <= 880 && _h <= 550){
				this.winWidth = 880;
				this.winHeight = 550;
				$(document.body).css({
					"width" : 880,
					"height" : 550
				});
			}else if(_w > 880 && _h <= 550){
				this.winWidth = _w;
				this.winHeight = 550;
				$(document.body).css({
					"width" : _w,
					"height" : 550
				});
			}else if(_w <= 880 && _h > 550){
				this.winWidth = 880;
				this.winHeight = _h;
				$(document.body).css({
					"width" : 880,
					"height" : _h
				});
			}else if(_w > 880 && _h > 550){
				this.winWidth = _w;
				this.winHeight = _h;
				$(document.body).css({
					"width" : _w,
					"height" : _h

				});
			}

			//默认布局配置
			defaultOpts = {
				pptCss : {
					"visibility" : "visible",
					"width" : this.winHeight-137,
					"height" : this.winHeight-214
				},
				pptAnim : {
					"left" : this.winWidth/2 - (this.winHeight-137)/2,
					"top" : 40
				},
				pptIsMain : true,
				qaCss : {
					"visibility" : "visible"
				},
				qaAnim : {
					"width" : (this.winHeight-157)/2 -8,
					"height" : this.winHeight -157,
					"left" : (this.winWidth/2 - (this.winHeight-137)/2)- ((this.winHeight-157)/2 -8) - 15,
					"top" : 40
				},
				videoCss : {
					"visibility" : "visible",
					"width" : (this.winHeight-157)/2 -8,
					"height" : (this.winHeight-157)/2 -8
				},
				videoAnim : {
					"left" : this.winWidth/2 - (this.winHeight-137)/2 + this.winHeight-137 +15,
					"top" : 40
				},
				outlineCss : {
					"visibility" : "visible"
				},
				outlineAnim : {
					"width" : (this.winHeight-157)/2 -8,
					"height" : (this.winHeight-157)/2 -8,
					"left" : this.winWidth/2 - (this.winHeight-137)/2 + this.winHeight-137 +15,
					"top" : (this.winHeight-157)/2 + 48
				},
				textLiveCss : {
					"left" : 5,
					"top" : this.winHeight-150
				},
                ctrlBarCss : {
                    "visibility" : "visible",
                    "width" : this.winHeight-137
                },
                ctrlBarAnim : {
                    "left" : this.winWidth/2 - (this.winHeight-137)/2,
                    "top" : this.winHeight - 164
                }
			}
			widgetCssOpts = JSON.parse($.cookie('vodWidgetCssCookie'))||defaultOpts;
		},
		bgReSize : function(){
			$("#bgImg").css({
				"width" : this.winWidth,
				"height" : this.winHeight
			});
			$(".web").css({
				"width" : this.winWidth - 10,
				"height" : this.winHeight - 10
			});
		},
		pptSize : function(){
			$(".ppt-container").css(widgetCssOpts.pptCss);
			$(".ppt-container").stop(true,true).animate(widgetCssOpts.pptAnim,500);
			this.pptInnerSize();
		},
		outlineSize : function(){
			$(".outline-container").css(widgetCssOpts.outlineCss);
			$(".outline-container").stop(true,true).animate(widgetCssOpts.outlineAnim,500,function(){
				webplayer.outlineInnerSize();
			});
		},
		qaSize : function(){
			$(".qa-container").css(widgetCssOpts.qaCss);
			$(".qa-container").stop(true,true).animate(widgetCssOpts.qaAnim,500,function(){
				webplayer.qaInnerSize();
			});
		},
		videoSize : function(){
			$(".video-container").css(widgetCssOpts.videoCss);
			$(".video-container").stop(true,true).stop(true,true).animate(widgetCssOpts.videoAnim,500);
			this.videoInnerSize();
		},
		textLivePos : function(){
			$(".text-live-container").css(widgetCssOpts.textLiveCss);
		},
        ctrlBarPos : function(){
            var ctrlBar = $(".control-container")
            ctrlBar.css(widgetCssOpts.ctrlBarCss);
            ctrlBar.stop(true,true).animate(widgetCssOpts.ctrlBarAnim,500);
            var ctrlBarWidth = ctrlBar.width();
            if(ctrlBarWidth<=315 && ctrlBarWidth >235){
                ctrlBar.addClass("control-main-mini-lv1");
                ctrlBar.removeClass("control-main-mini-lv2");
            }else if(ctrlBarWidth<=235){
                ctrlBar.addClass("control-main-mini-lv2");
                ctrlBar.removeClass("control-main-mini-lv1");
            }else if(ctrlBarWidth > 315){
                ctrlBar.removeClass("control-main-mini-lv1");
                ctrlBar.removeClass("control-main-mini-lv2");
            }
        },
		//问答切换到右边
		qaRightSize : function(){
			$(".qa-container").css({"visibility" : "visible"});
			$(".qa-container").stop(true,true).animate({
				"width" : (this.winHeight-157)/2 -8,
				"height" : (this.winHeight-157)/2 -8,
				"left" : this.winWidth/2 - (this.winHeight-137)/2 + this.winHeight-137 +15,
				"top" : (this.winHeight-157)/2 + 48
			},500,function(){
				webplayer.qaInnerSize();
			});

		},
		//大纲切换到左边
		outlineLeftSize : function(){
			$(".outline-container").css({"visibility" : "visible"});
			$(".outline-container").stop(true,true).animate({
				"width" : (this.winHeight-157)/2 -8,
				"height" : this.winHeight -157,
				"left" : (this.winWidth/2 - (this.winHeight-137)/2)- ((this.winHeight-157)/2 -8) - 15,
				"top" : 40
			},500,function(){
				webplayer.outlineInnerSize();
			});
		},

        //模块内部尺寸调整
        pptInnerSize : function(){
            $(".ppt-main").height(0);
            $(".ppt-main,.ppt-info-all").css({
                "height" : $(".ppt-container").height() - 30
            });
	        $(".ppt-info-inner").css({
                "height" : $(".ppt-container").height() - 76
            });
        },
        outlineInnerSize : function(){
            $(".outline-list").height(0);
            $(".outline-list").css({
                "height" :  $(".outline-container").height() -55
            });
        },
        qaInnerSize : function(){
            $("#qa-list, #leaveMsgList").height(0);
            $("#qa-list").css({
                "height" : $(".qa-container").height() - 30
            });
            $("#leaveMsgList").css({
                "height" : $(".qa-container").height() - 130
            });
        },
        videoInnerSize : function(){
            $(".player").height(0);
            $(".player").css({
                "height" : $(".video-container").height() - 30
            })

        },

		//使用cookie初始化布局
		cookieLayout : function(){
            if(widgetCssOpts.pptIsFullScreen || widgetCssOpts.videoIsFullScreen){
                webplayer.resizeInit();
            }else{
                this.pptSize();
                this.outlineSize();
                this.qaSize();
                this.videoSize();
                this.textLivePos();
                this.ctrlBarPos();
                $(".chat-container").removeClass("left-right-changed");

                $(".ppt-container")
                    .addClass(widgetCssOpts.pptIsMain ? "ppt-is-main" :"")
                    .removeClass("ppt-fullscreen")
                    .css("z-index","auto");
                $(".video-container")
                    .removeClass("v-full-screen")
                    .css("z-index","auto");
                $(".control-container").removeClass("control-fullscreen");
                ctrlBarAutoHide();//视频或文档全屏时resize浏览器，重新执行控制条自动隐藏方法，以便退出全屏时，不再自动隐藏
            }
		},

		//默认布局
		defaultLayout : function(){
			widgetCssOpts = defaultOpts;
			this.cookieLayout();
		},

		//左右互换
		leftRightChange : function(){
			widgetCssOpts = defaultOpts;
			var outlinePosLeft = $(".outline-container").offset().left;
			var qaPosLeft = $(".qa-container").offset().left;
			if(qaPosLeft<outlinePosLeft){
				this.qaRightSize();
				this.outlineLeftSize();
			}else{
				this.qaSize();
				this.outlineSize();
			}
			this.pptSize();
			this.videoSize();
            this.ctrlBarPos();
			$(".ppt-container").addClass("ppt-is-main");
		},

		//简易布局
		easyLayout : function(){

			$(".qa-container").css({"visibility" : "hidden"});
			$(".ppt-container").css({
				"visibility" : "visible",
				"width" : this.winHeight-137,
				"height" : this.winHeight-214
			});
			$(".ppt-container").stop(true,true).animate({
				"left" : (this.winWidth - (this.winHeight-137) - (this.winHeight-157)*3/5 - 15)/2,
				"top" : 40
			},500);
			$(".ppt-container").addClass("ppt-is-main");

			$(".video-container").css({
				"visibility" : "visible",
				"width" : (this.winHeight-157)*3/5,
				"height" : (this.winHeight-157)*3/5
			});
			$(".video-container").stop(true,true).animate({
				"left" : (this.winWidth - (this.winHeight-137) - (this.winHeight-157)*3/5 - 15)/2 + this.winHeight-122,
				"top" : 40
			},500);

			$(".outline-container").css({
				"visibility" : "visible",
				"width" : (this.winHeight-157)*3/5,
				"height" : this.winHeight-157 - (this.winHeight-157)*3/5 - 15
			});
			$(".outline-container").stop(true,true).animate({
				"left" : (this.winWidth - (this.winHeight-137) - (this.winHeight-157)*3/5 - 15)/2 + this.winHeight-122,
				"top" : (this.winHeight-157)*3/5 + 55
			},500,function(){
				webplayer.outlineInnerSize();
			});

            $(".control-container").css({
                "visibility" : "visible",
                "width" : this.winHeight-137
            });
            $(".control-container").stop(true,true).animate({
                "left" : (this.winWidth - (this.winHeight-137) - (this.winHeight-157)*3/5 - 15)/2,
                "top" : this.winHeight-164
            },500);


			this.pptInnerSize();
			this.videoInnerSize();

		}
	}

})(window);

//设置cookie
function setCookie(){
	$.cookie('vodWidgetCssCookie', getWidgetLastCss(), { expires: 7, path: '/' });
}